<?php
/**
 * SendTaskHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/12/11- 下午3:21
 */

namespace common\helpers;


use common\exception\TaskException;
use Swoole\Client;
use Yii;
class SendTaskHelper
{

    /**
     * 投递复制任务
     * @param array $args
     * @throws TaskException
     */
    public static function toCopy(array $args)
    {
        try {
            /**
             * 继续投递到异步任务中，再次去获取详细信息
             */
            $copyClient = new Client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_SYNC);
            $ret = $copyClient->connect(Yii::$app->params['copy_task']['ip'],Yii::$app->params['copy_task']['port'], 0.5, 0);
            if(!$ret)
            {
                throw new TaskException('复制服务暂未开启，请联系管理员开起');
            }
            $copyClient->send(json_encode($args,JSON_UNESCAPED_UNICODE));

            $copyClient->close();
        }catch (\Exception $e)
        {
            throw new TaskException('连接上货服务异常,请联系管理员');
        }

    }



    /**
     * 任务投递
     * @param array $args
     * @throws TaskException
     */
    public static function toSend(array $args)
    {
        try {
            /**
             * 继续投递到异步任务中，再次去获取详细信息
             */
            $client = new Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
            $ret = $client->connect(Yii::$app->params['swoole_task']['ip'],Yii::$app->params['swoole_task']['port'], 0.5, 0);

            if(!$ret)
            {
                throw new TaskException('同步服务暂未开启，请联系管理员开起');
            }

            //同步商品任务
            $client->send(json_encode($args,JSON_UNESCAPED_UNICODE));

            $client->close();
        }catch (\Exception $e)
        {
            throw new TaskException('连接任务服务异常,请联系管理员');
        }

    }
}